package punto2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Punto2 {
int cant;
static BufferedReader leer = new BufferedReader(new InputStreamReader(System.in));
	public Punto2() throws  IOException {	
		
		Nodo tope1,tope2;
		tope1=null;
		tope2=null;				
		tope1=ingresar(tope1);
		imprimir(tope1);
		tope2=ordenar(tope1);
		imprimir(tope2);
	}
	

	
		
	
		private Nodo ordenar(Nodo tope1) throws NumberFormatException, IOException {
		Nodo tope = new Nodo();
		tope=tope1;
		int[] vec = new int[cant];
		int op;
		do{
			System.out.print("\ningrese opcion de ordenamiento");
			System.out.print("\n	1.descendente");
			System.out.print("\n	2.ascendente:\n");
			op = Integer.parseInt(leer.readLine());
			
			switch(op){
			case 1:	
			vec=convertir(tope);			
			vec=descendente(vec);
			tope=apilar(vec);			
			break;
			case 2: 	
			vec=convertir(tope);	
			vec=ascendente(vec);
			tope=apilar(vec);			
			break;
			default: System.out.print("\nerror solo opciones 1 o 2\n");break;
			}	
			}while(op!=1 && op!=2);
		
		return tope;
	}	
			private Nodo apilar(int[] vec) {
			Nodo tope = new Nodo();
			tope=null;
			Nodo pila;
			for(int i=0;i<cant;i++){
				pila=new Nodo();
				if(tope==null)
					tope=pila;
				else
					pila.anterior=tope;
				
				pila.inf=vec[i];
				tope=pila;
			}
			
			
			
			return tope;
		}





			private int[] ascendente(int[] vec) {
				for(int k=1;k<cant;k++) {
	                for(int f=0;f<cant-k;f++) {
	                    if (vec[f]<vec[f+1]) {
	                        int aux;
	                        aux=vec[f];
	                        vec[f]=vec[f+1];
	                        vec[f+1]=aux;
	                    }
	                }
	            }
			return vec;
		}





			private int[] descendente(int[] vec) {
			
				for(int k=1;k<cant;k++) {
	                for(int f=0;f<cant-k;f++) {
	                    if (vec[f]>vec[f+1]) {
	                        int aux;
	                        aux=vec[f];
	                        vec[f]=vec[f+1];
	                        vec[f+1]=aux;
	                    }
	                }
	            }
			return vec;
		}
			private int[] convertir(Nodo tope1) {
			int vect[] = new int[cant];
			Nodo aux=new Nodo();
			aux=tope1;
			int ind=0;
			
			while(aux!=null){
				vect[ind]=aux.inf;
				aux=aux.anterior;
				ind++;
				
			}
			
			return vect;
		}
			
		private void imprimir(Nodo tope1) {
		Nodo aux = new Nodo();
		aux=tope1;
		System.out.print("\n\n");
		while(aux!=null){
			System.out.print("|"+aux.inf);
			aux=aux.anterior;
		}
		
	}





		private Nodo ingresar(Nodo tope1) throws NumberFormatException, IOException {
			Nodo tope=new Nodo();
			tope=null;
			int dato;
			Nodo pila;
			System.out.println("pulse 00 para salir");
			do{
				System.out.println("ingrese numero :");
				dato=Integer.parseInt(leer.readLine());
				if(dato!=00){
				pila = new Nodo();
				if(tope==null)
					tope=pila;
				else
					pila.anterior=tope;
				
				pila.inf=dato;
				tope=pila;
				cant++;
				}	
			}while(dato!=00);
		return tope;
	}
}
